package com.zmq.service.menu.impl;

import com.zmq.bean.MenuBean;
import com.zmq.dao.IMenuDao;
import com.zmq.service.menu.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("menuService")
public class MenuServiceImpl implements MenuService {

    @Autowired
    private IMenuDao menuDao;

    @Override
    public int addMenu(MenuBean menuBean) {
        return menuDao.insertSelective(menuBean);
    }

    @Override
    public int deleteMenu(MenuBean menuBean) {
        return menuDao.deleteByPrimaryKey(menuBean.getId());
    }

    @Override
    public int updateMenu(MenuBean menuBean) {
        return menuDao.updateByPrimaryKeySelective(menuBean);
    }

    @Override
    public List<Map<String, Object>> queryMenu() {
        List<MenuBean> list = menuDao.queryMenu();
        //return getzTreeDate(list,"0");
        return getTreeDate(list, "0",false,false,false);
    }

    /**
     * 递归遍历数据，生成ztree树的标准 JSON 数据
     * @param list
     * @param pid
     * @return
     */
    private List<Map<String,Object>> getzTreeDate(List<MenuBean> list, String pid){
        List<Map<String,Object>> treeData = new ArrayList<Map<String,Object>>();

        for (MenuBean menu:list){
            Map<String, Object> map = null;

            if(pid.equals(menu.getPid().toString())){
                map = new HashMap<String, Object>();

                map.put("id",menu.getId());
                map.put("pId",menu.getPid());
                map.put("name",menu.getMenuname());
                map.put("url",menu.getUrl());
                map.put("icon",menu.getIcon());
                map.put("roleid",menu.getRoleid());
                //给树根设置图标
//                if ("0".equals(menu.getPid())) {
//                    map.put("iconOpen", "/static/commons/orgimg/1_open.png");
//                    map.put("iconClose", "/static/commons/orgimg/organ.png");
//                }
                map.put("children", getzTreeDate(list, menu.getId().toString()));

            }
            if (map != null) {
                treeData.add(map);
            }
        }

        return treeData;
    }

    /**
     * 返回layui的树的json
     *
     * getTreeDate(list, "0",false,false,false);
     * @param list
     * @param pid
     * @param spread    //节点是否初始展开
     * @param checked   //节点是否初始为选中状态
     * @param disabled  //节点是否为禁用状态
     * @return
     */
    private List<Map<String,Object>> getTreeDate(List<MenuBean> list, String pid,boolean spread,boolean checked,boolean disabled){
        List<Map<String,Object>> treeData = new ArrayList<Map<String,Object>>();

        for (MenuBean menu:list){
            Map<String, Object> map = null;

            if(pid.equals(menu.getPid().toString())){
                map = new HashMap<String, Object>();

                map.put("id",menu.getId());
                map.put("pId",menu.getPid());
                map.put("title",menu.getMenuname());
                map.put("href",menu.getUrl());
                map.put("icon",menu.getIcon());

                //设置根目录展开
                if ("0".equals(menu.getPid())) {
                    map.put("spread",true);
                    map.put("checked",true);
                }else{
                    map.put("spread",spread);
                    map.put("checked",checked);
                }

                map.put("disabled",disabled);
                map.put("roleid",menu.getRoleid());

                map.put("children", getTreeDate(list, menu.getId().toString(),false,false,false));

            }
            if (map != null) {
                treeData.add(map);
            }
        }

        return treeData;
    }
}
